<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="卡密" prop="cdkey">
      <el-input v-model="dataForm.cdkey" placeholder="卡密"></el-input>
    </el-form-item>
    <el-form-item label="卡密状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="卡密状态"></el-input>
    </el-form-item>
    <el-form-item label="cdk类型" prop="cdkType">
      <el-input v-model="dataForm.cdkType" placeholder="cdk类型"></el-input>
    </el-form-item>
    <el-form-item label="使用时间" prop="employDate">
      <el-input v-model="dataForm.employDate" placeholder="使用时间"></el-input>
    </el-form-item>
    <el-form-item label="失效时间" prop="ineffectiveDate">
      <el-input v-model="dataForm.ineffectiveDate" placeholder="失效时间"></el-input>
    </el-form-item>
    <el-form-item label="卡密日期类型" prop="cdkDateType">
      <el-input v-model="dataForm.cdkDateType" placeholder="卡密日期类型"></el-input>
    </el-form-item>
    <el-form-item label="激活状态" prop="activatedStatus">
      <el-input v-model="dataForm.activatedStatus" placeholder="激活状态"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdDate">
      <el-input v-model="dataForm.createdDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="最后更新人" prop="lastUpdatedBy">
      <el-input v-model="dataForm.lastUpdatedBy" placeholder="最后更新人"></el-input>
    </el-form-item>
    <el-form-item label="最后更新时间" prop="lastUpdatedDate">
      <el-input v-model="dataForm.lastUpdatedDate" placeholder="最后更新时间"></el-input>
    </el-form-item>
    <el-form-item label="代理情况" prop="agenticState">
      <el-input v-model="dataForm.agenticState" placeholder="代理情况"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          remark: '',
          cdkey: '',
          status: '',
          cdkType: '',
          employDate: '',
          ineffectiveDate: '',
          cdkDateType: '',
          activatedStatus: '',
          createdBy: '',
          createdDate: '',
          lastUpdatedBy: '',
          lastUpdatedDate: '',
          agenticState: ''
        },
        dataRule: {
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          cdkey: [
            { required: true, message: '卡密不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '卡密状态', trigger: 'blur' }
          ],
          cdkType: [
            { required: true, message: 'cdk类型', trigger: 'blur' }
          ],
          employDate: [
            { required: true, message: '使用时间不能为空', trigger: 'blur' }
          ],
          ineffectiveDate: [
            { required: true, message: '失效时间不能为空', trigger: 'blur' }
          ],
          cdkDateType: [
            { required: true, message: '卡密日期类型', trigger: 'blur' }
          ],
          activatedStatus: [
            { required: true, message: '激活状态', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createdDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          lastUpdatedBy: [
            { required: true, message: '最后更新人不能为空', trigger: 'blur' }
          ],
          lastUpdatedDate: [
            { required: true, message: '最后更新时间不能为空', trigger: 'blur' }
          ],
          agenticState: [
            { required: true, message: '代理情况', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkcards/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.remark = data.amgcdkcards.remark
                this.dataForm.cdkey = data.amgcdkcards.cdkey
                this.dataForm.status = data.amgcdkcards.status
                this.dataForm.cdkType = data.amgcdkcards.cdkType
                this.dataForm.employDate = data.amgcdkcards.employDate
                this.dataForm.ineffectiveDate = data.amgcdkcards.ineffectiveDate
                this.dataForm.cdkDateType = data.amgcdkcards.cdkDateType
                this.dataForm.activatedStatus = data.amgcdkcards.activatedStatus
                this.dataForm.createdBy = data.amgcdkcards.createdBy
                this.dataForm.createdDate = data.amgcdkcards.createdDate
                this.dataForm.lastUpdatedBy = data.amgcdkcards.lastUpdatedBy
                this.dataForm.lastUpdatedDate = data.amgcdkcards.lastUpdatedDate
                this.dataForm.agenticState = data.amgcdkcards.agenticState
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkcards/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'remark': this.dataForm.remark,
                'cdkey': this.dataForm.cdkey,
                'status': this.dataForm.status,
                'cdkType': this.dataForm.cdkType,
                'employDate': this.dataForm.employDate,
                'ineffectiveDate': this.dataForm.ineffectiveDate,
                'cdkDateType': this.dataForm.cdkDateType,
                'activatedStatus': this.dataForm.activatedStatus,
                'createdBy': this.dataForm.createdBy,
                'createdDate': this.dataForm.createdDate,
                'lastUpdatedBy': this.dataForm.lastUpdatedBy,
                'lastUpdatedDate': this.dataForm.lastUpdatedDate,
                'agenticState': this.dataForm.agenticState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
